const axios = require('axios');
const nodeXlsx = require('node-xlsx')
//下方ex1是读取出来的数组，数组长度取决于Excel文件的工作表(sheet)
const ex1 = nodeXlsx.parse("./300gu.xlsx")	//读取excel表格
const cheerio = require('cheerio')
var xlsx = require('node-xlsx');
let count = 144

let excel_content = ex1[0].data	//取出excel文件中的第一个工作表中的全部数据

excel_content.splice(0, 1)	//一般来说表中的第一条数据可能是标题没有用，所以删掉

const HttpsProxyAgent = require("https-proxy-agent");

// 隧道域名和端口
let tunnelHost = 'j939.kdltps.com'
let tunnelPort = '15818'

// 配置用户名和密码
let username = 't17132945227397'
let password = '0mjdv12f'
let guname = ''

let titles = []
let times = []
let pageLimit = 500
const getPUrl = (code, page) => {
    return `http://guba.eastmoney.com/list,${code}_${page}.html`
}
let page = 1

const runTest = (code, page) => {
    let httpagt = new HttpsProxyAgent(`http://${username}:${password}@${tunnelHost}:${tunnelPort}`)
    let httpsagt = new HttpsProxyAgent(`http://${username}:${password}@${tunnelHost}:${tunnelPort}`)
    axios({
        url: `http://guba.eastmoney.com/list,${code}_${page}.html`,
        method: "get",
        headers: {
            referer: 'https://www.cvh.ac.cn'
        },
        httpAgent: httpagt,
        httpsAgent: httpsagt,
    }).then(
        res => {
            const $ = cheerio.load(res.data);
            $('.articleh .l3 a').each((i, el) => {
                titles.push($(el).text())
            })
            $('.articleh .l5').each((i, el) => {
                times.push($(el).text())
            })
            console.log(page)
            if (page < pageLimit + 1) {
                ++page
                runTest(code,page)
            } else {
                writeDir(guName, code)
            }
        }
    ).catch(err => {
        console.log(err);
    })
}
const writeDir =() => {
    const path1 = `${__dirname}/execlFile1/${guName}`
    var buffer
    var excelData = []
    var addInfo = {};
    //名称
    addInfo.name = guName;
    //数据数组
    addInfo.data = [
        ["序号", "股名", "评论标题", '时间'],
    ];
    fs.mkdir(path1, err => {
        titles.forEach((item, index) => {
            if (err) throw err
            addInfo.data.push([index + 1, guName, item, times[index]]);
        })
        //添加数据
        excelData.push(addInfo);
        // 写xlsx
        buffer = xlsx.build(excelData)
        try{ 
        //写入数据
        fs.writeFile(`${path1}/data.xls`, buffer, function (err) {
            if (err) {
                throw err;
            }
            titles = []
            times = []
            page = 1
            ++count
            start()
            console.log('Write to xls has finished');
        });
      }catch(err){console.log(err)}
    })
}
const startFetch = (guname, code) => {
    runTest(code, page)
    guname = guname
}
const start = () => {
    startFetch(excel_content[count - 1][2], excel_content[count - 1][1])
    // runTest(excel_content[count - 1][2], excel_content[count - 1][1])
}
start()